package com.jus.player.activity.my;

import static com.jus.player.api.OkHttpHelper._DATA;

import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONObject;
import com.google.android.gms.common.util.CollectionUtils;
import com.jus.player.R;
import com.jus.player.activity.BaseActivity;
import com.jus.player.adapter.RechargeDetailAdapter;
import com.jus.player.api.ApiConfig;
import com.jus.player.api.OkHttpHelper;
import com.jus.player.entity.RechargeDetail;
import com.jus.player.util.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 充值页面
 */
public class RechargeDetailActivity extends BaseActivity {

    private ImageView rechargeDetailBack;
    private RecyclerView recyclerView;
    private RechargeDetailAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_recharge_detail;
    }

    @Override
    protected void initView() {

        adapter = new RechargeDetailAdapter();
        rechargeDetailBack = findViewById(R.id.recharge_detail_back);
        recyclerView = findViewById(R.id.recharge_detail_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        rechargeDetailBack.setOnClickListener(view -> finish());
    }

    @Override
    protected void initData() {
        selectConsumeDetail();
    }

    /**
     * 请求网络接口：创建订单
     */
    private void selectConsumeDetail() {

        OkHttpHelper.getInstance(this).getAsync(ApiConfig.CONSUME_DETAIL, new HashMap<>(),
                new OkHttpHelper.HttpCallback() {
                    @Override
                    public void onSuccess(String res) {
                        JSONObject jo = JSONObject.parseObject(res);
                        List<RechargeDetail> list = jo.getList(_DATA, RechargeDetail.class);
                        if (!CollectionUtils.isEmpty(list)) {
                            runOnUiThread(() ->  adapter.setRechargeDetails(new ArrayList<>(list)));
                        }
                    }

                    @Override
                    public void onFailure(OkHttpHelper.HttpException e) {
                        CommonUtils.showToastAsync(RechargeDetailActivity.this, e.getMessage());
                    }
                });
    }

}